<?php
require_once( '../commandLine.inc' );

//    $quranFile    = 'quran-simple-modified.txt';   // quran file
		//echo ("starting....");
    $quranFile    = 'quran-uthmani-min.txt';   // quran file
    $metadataFile = 'quran-data.xml';  // quran metadata file
    $erabFile     = 'erab.txt';
    $user         = "Robot";
    $overwrite    = 0;
    $flags        = 0;
    $numText = file('numbers.txt');
    $arabic_numbers = explode(" ", $numText[0]);
    
    $surah_info = new SurahInfo();

		echo "<center> \n{| \n| \n";
    echo "{|class=\"wikitable\" style=\"margin-right:auto;margin-left:auto;\" \n";
		echo "!رد?ف \n!نام سوره \n!شمار آ?ه \n!ترت?ب نزول \n";
    for ($sura =1; $sura<=57; $sura++)
    {
      $sure_name  = $surah_info->get_surah_data($sura, "name");
			$sure_ayas  = $surah_info->get_surah_data($sura, "ayas");
			$sure_order = $surah_info->get_surah_data($sura, "order");
			echo "|- \n| $sura \n| [[سوره $sure_name | $sure_name]] \n| $sure_ayas \n| $sure_order \n";
		} 
		echo "|} \n| \n";
    echo "{|class=\"wikitable\" style=\"margin-right:auto;margin-left:auto;\" \n";
		echo "!رد?ف \n!نام سوره \n!شمار آ?ه \n!ترت?ب نزول \n|- \n\n";
    for ($sura =58; $sura<=114; $sura++)
    {
      $sure_name  = $surah_info->get_surah_data($sura, "name");
			$sure_ayas  = $surah_info->get_surah_data($sura, "ayas");
			$sure_order = $surah_info->get_surah_data($sura, "order");
			echo "|- \n| $sura \n| [[سوره $sure_name | $sure_name]] \n| $sure_ayas \n| $sure_order \n";
		} 
		echo "|} \n|} \n</center>";


class PageInfo
{
  var $pageTitle;
  var $pageContent;
  var $pageComment;

  function PageInfo()
  {
    global $user;
    global $flags;
	  //$user = User::newFromName( $user );
    //if (is_object($user))
		//  $wgUser =& $user;
		//$flags = 0 | ( isset( $options['norc'] ) ? EDIT_SUPPRESS_RC : 0 );
  }


  function write_to_wiki()
  {
    global $flags;
    global $overwrite;
	  $objTitle = Title::newFromUrl( $this->pageTitle );
    if (is_object($objTitle))
		  echo( "\nUsing title '" . $objTitle->getPrefixedText() . "'..." );
    else
    {
      echo( "\n############################## $this->pageTitle\n");
      exit(0);
    }
		
		if( is_object( $objTitle ) ) {
			
		  if( !$objTitle->exists() || $overwrite == 1 ) {
			
			  echo( "\nPerforming edit..." );
				$article = new Article( $objTitle );
				$article->doEdit( $this->pageContent, $this->pageComment, $flags );
				echo( "done.\n" );
				
			} else {
				echo( "page exists.\n" );
			}
			
		} else {
			echo( "invalid title.\n" );
	  }
  }

}	

class SurahInfo extends PageInfo
{
  var $suraData;

  function SurahInfo()
  {
    $this->PageInfo();
    global $metadataFile;
    $dataItems = Array("index", "start", "ayas", "name", "tname", "ename", "type", "rukus");

    $quranData = file_get_contents($metadataFile);
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, $quranData, $values, $index);
    xml_parser_free($parser);

    for ($i=1; $i<=114; $i++) 
    {
      $j = $index['SURA'][$i-1];
      foreach ($dataItems as $item)
      $this->suraData[$i][$item] = $values[$j]['attributes'][strtoupper($item)]; 
    }
  }

   // return given property of a sura
  function get_surah_data($sura, $property) 
  {
    return $this->suraData[$sura][$property]; 
  }

  function setPageTitle($sura)
  {
    $this->pageTitle = "سوره " . $this->suraData[$sura]["name"];
  }

  function setPageContent($sura, $ayats)
  {
    $this->pageContent = $ayats;
    $this->pageContent .= "\n==شان نزول==";
  }

  function setPageComment($sura)
  {
    $this->pageComment = "Edited by Robot v0.9";
  }

  function set_wiki_content($sura, $ayats)
  {
    $this->setPageTitle($sura);
    $this->setPageContent($sura, $ayats);
    $this->setPageComment($sura);
  }

  function get_ayahs_index($sura)
  {
    return Array($this->suraData[$sura]["start"], $this->suraData[$sura]["ayas"]);
  }
}

class AyahInfo extends PageInfo
{
  var $quran;

  function AyahInfo()
  {
    global $quranFile;
    $this->PageInfo();
    $this->quran = file($quranFile);
  }

  function get_ayahs($position)
  {
    $text = array_slice($this->quran, $position[0], $position[1]); 
    return $text;
  }

  function setPageTitle($surah, $num)
  {
    global $arabic_numbers;
    $this->pageTitle = $surah . " " . $arabic_numbers[$num];
  }

  function setPageContent($ayah)
  {
    //$ayah = trim($ayah);
    $this->pageContent = "=='''" . $ayah . "'''==\n";
    $this->pageContent .= "'''ترجمه تحت‌الفظي: '''\n";
    $this->pageContent .= "__TOC__\n";
    $this->pageContent .= "==ساختار زباني==\n";
  }

  function setPageComment($surah, $num)
  {
    $this->pageComment = "Edited by Robot v0.9";
  }

  function set_wiki_content($surah, $ayah, $num)
  {
    $this->setPageTitle($surah, $num);
    $this->setPageContent($ayah);
    $this->setPageComment($surah, $num);
  }
}

class WordInfo extends PageInfo
{
  var $erab;

  function WordInfo()
  {
    global $erabFile;
    $this->PageInfo();
    $text = file($erabFile);
    $this->erab = explode(" ", $text[0]);
  }

	function normalize($word)
  {
    $len = strlen($word);
    if ($len <= 4)
      return $word;
    $lastChar = substr($word, $len-2);
    if (in_array($lastChar, $this->erab))
      return substr($word, 0, $len-2);
    else
		  return $word;
	}

  function setPageTitle($word)
  {
    $this->pageTitle = trim($word);
  }

  function setPageContent($word)
  {
    $word = trim($word);
    $this->pageContent = "'''ترجمه تحت‌الفظي: '''\n";
    $this->pageContent .= "__TOC__\n";
    $this->pageContent .= "==ساختار زباني==\n";
  }

  function setPageComment()
  {
    $this->pageComment = "Edited by Robot V0.9";
  }

  function set_wiki_content($word)
  {
    $this->setPageTitle($word);
    $this->setPageContent($word);
    $this->setPageComment();
  }
}

/*
        $showBismillah = false; // change to true to show Bismillahs
        foreach ($suraText as $aya)
        {
            // remove bismillahs, except for suras 1 and 9
            if (!$showBismillah && $ayaNum == 1 && $sura !=1 && $sura !=9)
                $aya = preg_replace('/^(([^ ]+ ){4})/u', '', $aya);
            // display waqf marks in different style
           // $aya = preg_replace('/ ([U?-U?])/u', '<span class="sign">&nbsp;$1</span>', $aya);
 */
?>
